﻿''' <summary>
''' 设备组
''' </summary>
''' <remarks></remarks>
Public Class DeviceGroup

    ''' <summary>
    ''' 设备列表
    ''' </summary>
    ''' <remarks></remarks>
    Private Devs As Hashtable

    ''' <summary>
    ''' 设备的顶点坐标
    ''' </summary>
    ''' <remarks></remarks>
    Private iTop As Integer = 0
    ''' <summary>
    ''' 机组编号
    ''' </summary>
    ''' <remarks></remarks>
    Private iDevicesGroupsId = 0

    ''' <summary>
    ''' 板块编号
    ''' </summary>
    ''' <remarks></remarks>
    Private iBordId As Integer = 50

    Private iDxt As Integer = 1
    Private iDyt As Integer = 1

    ''' <summary>
    ''' 设备类型
    ''' </summary>
    ''' <remarks></remarks>
    Private _GroupType As DeviceTypes = DeviceTypes.Compressor


    ''' <summary>
    ''' 使用默认的构造函数
    ''' </summary>
    ''' <remarks>初始设备类型为DeviceTypes.Compressor</remarks>
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        Me.iDevicesGroupsId = 1
        Me._GroupType = DeviceTypes.Compressor
        '' 初始化组
        InitGroup()
        InitGroupName()
    End Sub

    ''' <summary>
    ''' 使用设备类型进行初始化
    ''' </summary>
    ''' <param name="dgId" >机组编号</param>
    ''' <param name="bordId">EX版编号</param>
    ''' <param name="dType">设备类型</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal dgId As Integer, ByVal bordId As Integer, ByVal dType As DeviceTypes)

        InitializeComponent()

        Me.iDevicesGroupsId = dgId
        Me._GroupType = dType
        Me.iBordId = bordId
        '' 初始化组
        InitGroup()
        InitGroupName()
    End Sub

    ''' <summary>
    ''' 初始化组
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitGroup()
        Me.Devs = New Hashtable


        Dim ds As DataSet = Nothing
        ds = GetGroupInfo()

        If (ds IsNot Nothing) AndAlso (ds.Tables(0).Rows.Count > 0) Then
            Dim dr As DataRow
            If (_GroupType = DeviceTypes.Compressor) Then
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    dr = ds.Tables(0).Rows(i)
                    '' 创建设备
                    CreateDevice(CInt(dr(0)), dr(2).ToString(), Me.iBordId)
                Next
            Else
                For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
                    dr = ds.Tables(0).Rows(i)
                    '' 创建设备
                    CreateDevice(CInt(dr(0)), dr(2).ToString(), CInt(dr(3)))
                Next
            End If

        End If

        '' 重新设置控件大小
        ReSizeControl()
    End Sub

    ''' <summary>
    ''' 创建设备
    ''' </summary>
    ''' <param name="devId">设备编号</param>
    ''' <param name="name">设备名称</param>
    ''' <remarks></remarks>
    Private Sub CreateDevice(ByVal devId As Integer, ByVal name As String, ByVal brodid As Integer)
        Dim dev As DeviceItem = New DeviceItem(devId, brodid, name, Me._GroupType)
        If (dev IsNot Nothing) Then

            dev.Left = iDxt
            dev.Top = iTop + iDyt

            iTop += dev.Height
            Me.pnlDevs.Controls.Add(dev)

            ''==============================
            '' 添加端口
            Dim ps As Hashtable = dev.GetPorts()
            If ps.Count > 0 Then
                For Each key As Integer In ps.Keys
                    Try
                        Devs.Add(key, ps(key))
                    Catch

                    End Try
                Next
            End If
        End If
    End Sub

    ''' <summary>
    ''' 更具类型获取数据库中相应的记录
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetGroupInfo() As DataSet
        Dim ds As DataSet = Nothing


        Select Case Me._GroupType
            Case DeviceTypes.Compressor
                Dim dal As CcapCompressorItemService = New CcapCompressorItemService()
                ds = dal.GetCompressor(iDevicesGroupsId)
            Case DeviceTypes.Condenser
                Dim dal As CcapCondenserItemService = New CcapCondenserItemService()
                ds = dal.GetCondenser(iDevicesGroupsId)
            Case DeviceTypes.AirCooler
                Dim dal As CcapAirCoolerItemService = New CcapAirCoolerItemService()
                ds = dal.GetAirCooler(iDevicesGroupsId)
        End Select

        Return ds
    End Function

    ''' <summary>
    ''' 初始化类型组名称
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitGroupName()
        Select Case Me._GroupType
            Case DeviceTypes.Compressor
                Me.lblGroupName.Text = "压缩机"
            Case DeviceTypes.Condenser
                Me.lblGroupName.Text = "冷凝器"
            Case DeviceTypes.AirCooler
                Me.lblGroupName.Text = "冷风机"
        End Select
    End Sub

    ''' <summary>
    ''' 从新计算控件的高度
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ReSizeControl()

        If Devs.Count > 0 Then
            Me.Height = iTop + 20 + iDyt
        Else
            Me.Height = 20 + 1 + Me.lblGroupName.Height
        End If

        Me.lblGroupName.Top = (Me.pnlName.Height - Me.lblGroupName.Height) / 2 - 5
    End Sub

    ''' <summary>
    ''' 获取端口
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetPorts() As Hashtable
        Return Me.Devs
    End Function
End Class

''' <summary>
''' 设备类型
''' </summary>
''' <remarks></remarks>
Public Enum DeviceTypes
    ''' <summary>
    ''' 压缩机
    ''' </summary>
    ''' <remarks></remarks>
    Compressor = 1

    ''' <summary>
    ''' 冷凝器
    ''' </summary>
    ''' <remarks></remarks>
    Condenser = 2

    ''' <summary>
    ''' 冷风机
    ''' </summary>
    ''' <remarks></remarks>
    AirCooler = 3
End Enum
